package com.hz.service.impl;

import com.hz.mapper.CategoryMapper;
import com.hz.pojo.Category;
import com.hz.service.CategoryService;
import com.hz.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public int addCategory(Category category) {
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());

        Map<String,Object> map = ThreadLocalUtil.get();

        Integer id = (Integer) map.get("id");

        category.setCreateUser(id);
        return categoryMapper.insert(category);

    }

    @Override
    public Category findByName(String categoryName) {
        return categoryMapper.selectByName(categoryName);
    }

    @Override
    public List<Category> listCategory(Integer id) {
        return categoryMapper.selectByUserId(id);
    }

    @Override
    public Category findCategory(Integer id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public int updateCategory(Integer id, String categoryName, String categoryAlias) {
        return categoryMapper.updateCategory(id,categoryName,categoryAlias);
    }

    @Override
    public int deleteByID(Integer id) {
        return categoryMapper.deleteById(id);
    }
}
